<%@ page language="java" %>
<%
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("max-age", 0);
    response.setDateHeader("Expires", 0);
%>

<%@ page import="com.scalar.chiptrack.utils.ConfigManager,
                 com.scalar.chiptrack.users.User,
                 com.scalar.chiptrack.commons.SessionKeys"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>

<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>
<html>
<HEAD>
	<TITLE>ScalarSoft - ChipTrac - Wafer Data Entry </TITLE>
    <META http-equiv='Content-Type' content='text/html; charset=windows-1252'>
    <LINK href='<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css' type='text/css' rel='stylesheet'>
	<script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
    <script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>
    <script language='javascript' >

	function resetValues()
    {
        document.waferDataEntryForm.reset();
        onLoad();
    }

    function save()
	{
        var rowIndex = <%=request.getParameter("rowIndex")%>;
        var type = "<%=request.getParameter("type")%>";

        errors  = "";
        var pa  = document.getElementById("message");
        pa.innerHTML = '';
        success = false;

        var form		            = document.waferDataEntryForm;
		var openerForm	            = window.opener.document.forms[0];
        var openerFormwaferIdCtrl   = eval( openerForm['wipLotsList['+ rowIndex+'].waferIds'] );
        var openerFormsortedWaferIdsCtrl  = eval( openerForm['wipLotsList['+ rowIndex+'].splitLotOneWaferIds'] );
        var openerFormblindBuildWaferIdsCtrl  = eval( openerForm['wipLotsList['+ rowIndex+'].splitLotTwoWaferIds'] );
        var waferIdsValue           = openerFormwaferIdCtrl.value;
        if ( eval(waferIdsValue.lastIndexOf(";") + 1 ) == waferIdsValue.length )
        {
             waferIdsValue = waferIdsValue.substring(0, eval(waferIdsValue.length-1));
        }
        var sorttedWaferArry;
        var blindBuildWaferArry;

        if (openerFormsortedWaferIdsCtrl.value != null && openerFormsortedWaferIdsCtrl.value != '')
           sorttedWaferArry =  openerFormsortedWaferIdsCtrl.value.split(";");
        
        if (openerFormblindBuildWaferIdsCtrl.value != null && openerFormblindBuildWaferIdsCtrl.value != '')
           blindBuildWaferArry =  openerFormblindBuildWaferIdsCtrl.value.split(";");

        var waferIdsArray      = waferIdsValue.split(";");
        var waferIdCtrl        =  form.waferId;

        if ( waferIdCtrl.length != null )
        {
            var waferIds = "";
            var lengthOfSelectedWaferIds = 0;

            for ( i = 0; i < waferIdCtrl.length; i++ )
            {
                if( waferIdCtrl[i].checked == true )
                {
                    lengthOfSelectedWaferIds++;
                }
            }
            if ( (waferIdsValue == null || waferIdsValue == '' ) && eval(lengthOfSelectedWaferIds) == 0 )
            {
                 pa.innerHTML = "<li>Wafers Info not available; Please select Wafers</li>";
                 return;
            }
            if( eval(lengthOfSelectedWaferIds) == 0 )
            {
                pa.innerHTML = "<li>Please select Wafers</li>";
                return;
            }
            if( eval(waferIdsValue) < eval(lengthOfSelectedWaferIds) )
            {
                pa.innerHTML = "<li>Wafers selected should not be more than the avialable Qty <font color='blue'> "+ eval(openerFormsplitLotOneWaferQtyIn) + " </font></li>";
                return;
            }

            //START: ASSIGNING WAFER IDS TO THE PARENT WINDOW
            var splitLotOnewaferIds = "";
            var splitLotTwowaferIds = "";
            var isSameWaferIds      = false;
            var isSortWaferIds      = false;
            var isblindBuildWaferIds = false;
            var selectedWafer      ="";

            if (waferIdsValue != null && waferIdsValue != '' )
            {
                for ( var i = 0; i < waferIdCtrl.length; i++ )
                {
                    if( waferIdCtrl[i].checked == true )
                    {
                        for (var k =0; k < waferIdsArray.length; k++)
                        {
                             if ( waferIdCtrl[i].value == waferIdsArray[k] )
                             {
                                 isSameWaferIds = true;
                                 break;
                             }
                             else
                             {
                                 isSameWaferIds = false;
                             }
                        }
                        if ( type != null && type == "sortedWafer")
                        {
                            if ( blindBuildWaferArry != null && blindBuildWaferArry != '')
                            {
                                for (var s =0; s < blindBuildWaferArry.length; s++)
                                {
                                     if ( ( i + 1 ) == sorttedWaferArry[s] )
                                     {
                                         isblindBuildWaferIds = true;
                                         selectedWafer = sorttedWaferArry[s];
                                         break;
                                     }
                                }
                            }   
                            splitLotOnewaferIds = splitLotOnewaferIds + ( i + 1 ) + ";" ;
                        }
                        else if ( type != null && type == "blindBuild")
                        {
                            if ( sorttedWaferArry != null && sorttedWaferArry != '')
                            {
                                for (var b =0; b < sorttedWaferArry.length; b++)
                                {
                                     if ( ( i + 1 ) == sorttedWaferArry[b] )
                                     {
                                         isSortWaferIds = true;
                                         selectedWafer  = sorttedWaferArry[b];
                                         break;
                                     }
                                }
                            }
                            splitLotTwowaferIds = splitLotTwowaferIds + ( i + 1 ) + ";" ;
                        }
                    }
                }
            }
            else
            {
                isSameWaferIds = true;
                for ( i = 0; i < waferIdCtrl.length; i++ )
                {
                    if( waferIdCtrl[i].checked == true )
                    {
                        if ( type != null && type =="sortedWafer")
                        {
                            splitLotOnewaferIds = splitLotOnewaferIds + ( i + 1 ) + ";" ;
                        }
                        else if ( type != null && type == "blindBuild")
                        {
                            splitLotTwowaferIds = splitLotTwowaferIds + ( i + 1 ) + ";" ;
                        }
                    }
                }
            }

            if ( isSameWaferIds == false)
            {
                pa.innerHTML = "<li> Please select wafers from available wafer ids : <font color='blue'>" + waferIdsValue + "</font></li>";
                return;
            }
            if ( isSortWaferIds)
            {
                pa.innerHTML = "<li> Wafer already selected as Sorted Wafer : <font color='blue'> " +selectedWafer + "</font></li>";
                return;
            }
            if ( isblindBuildWaferIds )
            {
                pa.innerHTML = "<li> Wafer already selected as Blind Build Wafer : <font color='blue'> " +selectedWafer + "</font></li>";
                return;
            }
            if ( type != null && type =="sortedWafer")
            {
                
                openerFormsortedWaferIdsCtrl.value = splitLotOnewaferIds;
            }
            else if ( type != null && type == "blindBuild")
            {
                openerFormblindBuildWaferIdsCtrl.value = splitLotTwowaferIds;
            }
           
        }
        closeWindow();
    }

    function closeWindow()
    {
        window.close();
    }

    function onLoad()
    {
        var rowIndex = <%=request.getParameter("rowIndex")%>;
        var type = "<%=request.getParameter("type")%>";
        var pa  = document.getElementById("message");

        var form		            = document.waferDataEntryForm;
		var openerForm	            = window.opener.document.forms[0];
        var openerFormsortWaferIdsCtrl  = eval( openerForm['wipLotsList['+rowIndex+'].splitLotOneWaferIds'] );
        var openerFormblindBuildWaferIdsCtrl  = eval( openerForm['wipLotsList['+rowIndex+'].splitLotTwoWaferIds'] );
        var waferIdCtrl             = form.waferId;
        var sortwaferIdsValue       = openerFormsortWaferIdsCtrl.value;
        var blindBuildwaferIdsValue = openerFormblindBuildWaferIdsCtrl.value;
        var openerFormWaferIdsCtrl  = eval( openerForm['wipLotsList['+rowIndex+'].waferIds'] );
        var openerFormWaferIdsValue = openerFormWaferIdsCtrl.value;


        if ( type != null && type =="view")
        {
            if ( openerFormWaferIdsValue == null || openerFormWaferIdsValue == '' )
            {
                 for ( var i = 0; i < waferIdCtrl.length; i++ )
                 {
                      form.waferIds.disabled  = true;
                      waferIdCtrl[i].disabled = true;
                 }
                 pa.innerHTML = "<li>WaferId Info not available</li>";
                 return;
            }

            var waferIdsArray      = openerFormWaferIdsValue.split(";");
            form.waferIds.disabled  = true;
            for ( i = 0; i < waferIdCtrl.length; i++ )
            {
                for ( var j = 0; j < waferIdsArray.length; j++ )
                {
                     if ( waferIdCtrl[i].value == waferIdsArray[j] )
                     {
                         waferIdCtrl[i].checked = true;
                         waferIdCtrl[i].disabled = true;
                         break;
                     }
                     else
                     {
                         waferIdCtrl[i].disabled = true;
                     }
                }
            }
        }
        else if ( type != null && type =="sortedWafer")
        {

            if ( sortwaferIdsValue != null && eval(sortwaferIdsValue.lastIndexOf(";") +1 ) == sortwaferIdsValue.length )
            {
                 sortwaferIdsValue = sortwaferIdsValue.substring(0, eval(sortwaferIdsValue.length-1));
            }

            if ( sortwaferIdsValue == null || sortwaferIdsValue == '' )
            {

                 pa.innerHTML = "<li>WaferId Info not available; Please select Wafers</li>";
                 return;
            }
            var enteredWaferIdsArr      = sortwaferIdsValue.split(";");
            //START: TO POPULATE ALREADY ENTERED VALUES
            if ( waferIdCtrl != null )
            {
                if ( waferIdCtrl.length )
                {
                    var waferIdsCount           = waferIdCtrl.length;
                    var enteredwaferIdsCount    = enteredWaferIdsArr.length;

                    var minCount                = ( waferIdsCount > enteredwaferIdsCount ) ? enteredwaferIdsCount : waferIdsCount;

                    for ( i = 0; i < waferIdsCount; i++ )
                    {
                        for ( j = 0; j < minCount; j++ )
                        {
                            if( eval( enteredWaferIdsArr[j] -1 ) == i )
                            {
                                waferIdCtrl[i].checked = true;
                            }
                        }
                    }
                }
            }
        }
        else if ( type != null && type =="blindBuild")
        {

            if ( blindBuildwaferIdsValue !=null && eval(blindBuildwaferIdsValue.lastIndexOf(";") +1 ) == blindBuildwaferIdsValue.length )
            {
                 blindBuildwaferIdsValue = blindBuildwaferIdsValue.substring(0, eval(blindBuildwaferIdsValue.length-1));
            }

            if ( blindBuildwaferIdsValue == null || blindBuildwaferIdsValue == '' )
            {
                 pa.innerHTML = "<li>WaferId Info not available; Please select Wafers</li>";
                 return;
            }
            var enteredWaferIdsArr      = blindBuildwaferIdsValue.split(";");
            //START: TO POPULATE ALREADY ENTERED VALUES
            if ( waferIdCtrl != null )
            {
                if ( waferIdCtrl.length )
                {
                    var waferIdsCount           = waferIdCtrl.length;
                    var enteredwaferIdsCount    = enteredWaferIdsArr.length;
                    var minCount                = ( waferIdsCount > enteredwaferIdsCount ) ? enteredwaferIdsCount : waferIdsCount;

                    for ( i = 0; i < waferIdsCount; i++ )
                    {
                        for ( j = 0; j < minCount; j++ )
                        {
                            if( eval( enteredWaferIdsArr[j] -1 ) == i )
                            {
                                waferIdCtrl[i].checked = true;
                            }
                        }
                    }
                }
            }
        }

        //END
    }

    /*To select all the checkboxs and deselect the all checkboxes*/
    function setSelectAllWaferIds(Object)
    {
        var selId = document.waferDataEntryForm.waferId;
        var count = 0;
        var pa  = document.getElementById("message");

        if (typeof Object.length =='undefined')
        {
           pa.innerHTML = '';
           if( Object.checked == true )
           {
               document.waferDataEntryForm.waferId.checked = true;
               for ( count = 0; count < selId.length; count ++ )
               {
                    selId[count].checked = true;
               }
            }
            else
            {
               document.waferDataEntryForm.waferId.checked = false;
               for ( count = 0; count < selId.length; count ++ )
               {
                    selId[count].checked = false;
               }
            }
        }
    }

    /*if deselect the checkbox, to deselect the SelectAll checkbox*/
    function deSelectWaferIds()
    {
        var pa  = document.getElementById("message");
        pa.innerHTML = '';
        var selId = document.waferDataEntryForm.waferId;
        var selAll = document.waferDataEntryForm.waferIds;

        for( var i=0; i< selId.length;i++ )
        {
            if(selId[i].checked+""=='false')
            {
                document.waferDataEntryForm.waferIds.checked = false;
                return;
            }
            if(selId[i].checked+""=='true')
            {
                document.waferDataEntryForm.waferIds.checked = true;
            }
        }
    }

    </script>
</HEAD>
<%
    User user = ( User ) session.getAttribute ( SessionKeys.USER_KEY );
    if ( user != null )
    {
%>
<BODY bgcolor='ffffff' marginheight='0' marginwidth='0' topmargin='0' leftmargin='0' onLoad='onLoad()'>

    <TABLE align='center' border='0' width='100%' >
        <TR>
            <td align='center' class="NArialW">Wafer Data Entry</td>
        </TR>
        <TR>
            <TD align='center'>&nbsp;</TD>
        </TR>
        <form name="waferDataEntryForm" action="">
        <%
            String type =  request.getParameter( "type" ) ;
            int waferQty = 0;
            try
            {
                waferQty = Integer.parseInt( request.getParameter( "waferQty" ) );
            }
            catch( Exception ex )
            {
                waferQty = 0;
            }

            if( waferQty == 0 )
            {
        %>
        <TR>
            <TD align='center'><font class="NArialL" color="red">Wafer Qty Should be Greater zero / Not Entry </font></TD>
        </TR>
        <TR>
            <TD align='center'><font class="NArialL">&nbsp;</font></TD>
        </TR>
        <TR>
            <TD align='center'><a href="javascript:close()" title='Click here to close the window'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" ></a></TD>
        </TR>
        <%
            }
            else
            {
        %>
         <TR>
           <TD class="NArialL">
           <input type="checkbox" name="waferIds" styleClass="txfield" onClick="javascript:setSelectAllWaferIds(this)"/>
           <font class="NArialL">Select All &nbsp;</font>

           </TD>
         </TR>

        <TR>
            <TD align='center'>
			    <TABLE align="center" border="0" width="100%">
				    <TR>
				        <TD width="100%" colspan='5' class="NArialL">&nbsp;<FONT id="message" size=1 color='FF0000'></FONT></TD>
				    </TR>
            <%
                    for( int count = 0; count < 25 ; count++ )
                    {
            %>
            <%          if( count % 5 == 0 ) { %> <TR> <% } %>
                        <TD  class="NArialL">
                            <input type="checkbox" name="waferId" value = "<%=count+1%>" styleClass="txfield" onClick="javascript:deSelectWaferIds()"/>&nbsp;Wafer-<%=count+1%>&nbsp;&nbsp;&nbsp;
                        </TD>
            <%
                    }
            %>
                </TABLE>
            </TD>
        </TR>
        <TR><TD align='center'>&nbsp;</TD></TR>
         <%
             if ( type != null && type.equalsIgnoreCase("view"))
             {
         %>
         <TR><TD align='center'><a href="javascript:window.close()" title='Click here to close the window'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" ></a></TD></TR>
         <% }
         else
         { %>
        <TR>
            <TD align='center'>
                    <a href="javascript:save()" title='Click here to Save Wafer Data Entry'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" ></a>&nbsp;&nbsp;&nbsp;
                    <a href="javascript:resetValues()" title='Click here to reset all field values'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/reset.gif" ></a>&nbsp;&nbsp;&nbsp;
                    <a href="javascript:window.close()" title='Click here to close the window'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" ></a>
            </TD>
        </TR>

    <%   }
      }
    %>
        </form>
    </table>
</body>
<%
    }
    else
    {
%>
    <BODY bgcolor='ffffff' marginheight='0' marginwidth='0' topmargin='0' leftmargin='0' >
    <TABLE cellpadding='0' cellspacing='0' bgcolor='FDF5E6' border="0" width="100%"  height="100%" align=center>
        <TR>
            <TD>
                <TABLE align="center" border="0" width="40%" bgcolor="#FDF5E6">
                    <TR><TD>&nbsp;</TD></TR>
                    <TR><TD align="center" ><font class="NArialL" ><font color="red"><b>Session has been time out , Please close this window and login again.</b></font></font></TD></TR>
                    <TR><TD>&nbsp;</TD></TR>
                    <TR><TD align="center" ><a href="javascript:window.close();"><font class='NArialL'>[Close this window]</font></a></TD></TR>
                </TABLE>
            </TD>
       </TR>
       <TR><TD>&nbsp;</TD></TR>
    </TABLE>
    </BODY>
<% } %>
</html>
